//                 _              
//                | |             
//   __ _ _ __ ___| |__ __  _____ 
//  / _` | '__/ __| '_ \\ \/ / __|
// | (_| | | | (__| | | |>  <\__ \
//  \__,_|_|  \___|_| |_/_/\_\___/
//                                
// Copyright 2010 Scott Dixon http://www.archxs.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package com.archxs.commons.collections;

import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;

import com.archxs.commons.Variant;


/**
 * Sanity check that uses the {@link VariantPropertiesMapProxy} in some
 * typical use cases.
 */
public class VariantPropertiesMapProxyUseCaseTest {
    
    @Test
    public void testServletRequest() {
        final Map<String,String[]> servletRequestParameters = new HashMap<String,String[]>();
        servletRequestParameters.put("responseFormat", new String[]{"text/xml"});
        servletRequestParameters.put("cities", new String[]{"Toronto", "New York", "Denver", "London", "Cairo", "Moscow"});
        servletRequestParameters.put("neighborhoods", new String[]{"[\"High Park\", \"East Danforth\", \"Fort York\"]", "[\"The Upper East Side\", \"Soho\",\"Central Park\"]"});
        servletRequestParameters.put("appleProductionByYear", new String[]{ "15", "25", "445.3", "0xFF"});
        
        final VariantProperties props = VariantPropertiesMapProxy.proxyInstance(servletRequestParameters);
        Assert.assertEquals(4, props.size());
        // Single values attributes.
        final Variant responseFormat = props.get("responseFormat");
        Assert.assertNotNull(responseFormat);
        Assert.assertEquals("text/xml", responseFormat.toString());
        // Multi-valued attributes
        final Variant cities = props.get("cities");
        Assert.assertNotNull(cities);
        final Variant[] cityList = cities.getArray();
        Assert.assertEquals(6, cityList.length);
        // Array values
        final Variant neighborhoods = props.get("neighborhoods");
        Assert.assertNotNull(neighborhoods);
        Assert.assertEquals(2, neighborhoods.getArrayLength());
        final Variant[] neighborhoodList = neighborhoods.getArray();
        for(final Variant neighborhoodsOfCity : neighborhoodList) {
            Assert.assertEquals(3, neighborhoodsOfCity.getArrayLength());
        }
        // Numbers
        final Variant appleProduction = props.get("appleProductionByYear");
        Assert.assertNotNull(appleProduction);
        Assert.assertEquals(4, appleProduction.getArrayLength());
        for(final Variant applesProduced : appleProduction.getArray()) {
            Assert.assertTrue(applesProduced.longValue() > 0);
        }
    }
}